package de.unikoblenz.west.csxpoi;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

import de.unikoblenz.west.csxpoi.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

/**
 * DateTimePickerActivity shows a separate window, where the user can set date
 * and time and send it back to the poi editor activity.
 * 
 * @author Daniel Schmeiss
 * 
 */
public class DateTimePickerActivity extends Activity {

	private TextView mTextView;
	private DatePicker mDate;
	private TimePicker mTime;
	private Button mCancelButton, mSaveButton;

	private String mCaption = "";
	private boolean mFrom;
	private int mYear, mMonth, mDay, mHour, mMinutes;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.date_time_picker_activity);

		mTextView = (TextView) findViewById(R.id.TextView01);
		mDate = (DatePicker) findViewById(R.id.begin_date_poi_editor);
		mTime = (TimePicker) findViewById(R.id.begin_time_poi_editor);
		mCancelButton = (Button) findViewById(R.id.cancel_datetime_button);
		mSaveButton = (Button) findViewById(R.id.save_datetime_button);

		mTime.setIs24HourView(true);

		mDate.setVisibility(View.VISIBLE);
		mTime.setVisibility(View.VISIBLE);
		mTextView.setVisibility(View.VISIBLE);

		Bundle extras = getIntent().getExtras();
		// get the date and time out of the extras
		if (extras != null) {
			mCaption = extras.getString("caption");
			mFrom = extras.getBoolean("from");

			mYear = extras.getInt("year");
			mMonth = extras.getInt("month");
			mDay = extras.getInt("day");
			mHour = extras.getInt("hour");
			mMinutes = extras.getInt("minutes");
		}
		// if there are no extras, set the date to the current date/time
		else {
			Calendar cal = new GregorianCalendar();
			cal.setTimeZone(TimeZone.getDefault());
			mYear = cal.get(Calendar.YEAR);
			mMonth = cal.get(Calendar.MONTH);
			mDay = cal.get(Calendar.DAY_OF_MONTH);
			mHour = cal.get(Calendar.HOUR_OF_DAY);
			mMinutes = cal.get(Calendar.MINUTE);
		}

		mDate.updateDate(mYear, mMonth, mDay);
		mTime.setCurrentMinute(mMinutes);
		mTime.setCurrentHour(mHour);

		mTextView.setText(mCaption);

		mCancelButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		mSaveButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				returnResult();
			}
		});
	}

	/**
	 * returnResult creates and sends an intent to the {@link PoiEditorActivity}
	 * , that contains the chosen date and time.
	 */
	private void returnResult() {
		Intent resultIntent = new Intent(DateTimePickerActivity.this,
				PoiEditorActivity.class);
		resultIntent.putExtra("year", mDate.getYear());
		resultIntent.putExtra("month", mDate.getMonth());
		resultIntent.putExtra("day", mDate.getDayOfMonth());
		resultIntent.putExtra("hour", mTime.getCurrentHour().intValue());
		resultIntent.putExtra("minutes", mTime.getCurrentMinute().intValue());
		resultIntent.putExtra("from", mFrom);
		setResult(RESULT_OK, resultIntent);
		finish();
	}
}
